//
//  OrderDetailGoodsCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/6.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "OrderDetailGoodsCell.h"

#import "DetailGoodsTableViewCell.h"

@interface OrderDetailGoodsCell()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UILabel *titleLB;

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,assign) BOOL hiddenRefund;

@property (nonatomic,strong) UILabel *priceLB;//商品价格数量总计
@property (nonatomic,strong) UILabel *paymentLB;//商品实付款总计

@end

@implementation OrderDetailGoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = MainBackColor;
        self.contentView.backgroundColor = MainBackColor;
        self.hiddenRefund = NO;
        
        [self initUI];
    }
    return self;
}

#pragma mark - setter
- (void)setModel:(OrderDetailModel *)model{
    _model = model;
    if (_model.items.count > 0) {
        self.dataSourceArr = [NSMutableArray arrayWithArray:_model.items];
        
        float height = 0;
        
        for (ProductOrderModel *model1 in _model.items) {
            height = height + (model1.refundbtn == YES ? 140:110);
        }
        self.tableView.height = height;
        [self.tableView reloadData];
        
        self.priceLB.text = [NSString stringWithFormat:@"共%ld件商品  共计 %@",_model.number,[Configuration getTotalPriceWithPrice:_model.totalprice withTbPrice:_model.totaltbprice]];
        
        self.paymentLB.text = [NSString stringWithFormat:@"实付：%@",[Configuration getTotalPriceWithPrice:_model.payprice withTbPrice:_model.totaltbprice]];
        
        self.priceLB.top = self.tableView.bottom + 20;
        self.paymentLB.top = self.priceLB.bottom + 11;
        
        self.bgView.height = self.tableView.height + 95;
        float cellHeight = self.bgView.bottom;
        
        if (self.updateCellHeightBlock) {
            self.updateCellHeightBlock(cellHeight);
        }
    }
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.titleLB];
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.tableView];
    [self.bgView addSubview:self.priceLB];
    [self.bgView addSubview:self.paymentLB];
}
#pragma mark - lazy
- (UILabel *)titleLB{
    if (!_titleLB) {
        _titleLB = [[UILabel alloc] initWithFrame:CGRectMake(17, 23, 120, 20)];
        _titleLB.textColor = MainBlackTextColor;
        _titleLB.font = FontSet(14);
        _titleLB.textAlignment = NSTextAlignmentLeft;
        _titleLB.text = @"购买商品";
    }
    return _titleLB;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.titleLB.bottom + 8, ScreenWidth, 210)];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 12;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 110) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = MainBackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.scrollEnabled = NO;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
        [_tableView setTableHeaderView:view];
    }
    return _tableView;
}

- (UILabel *)priceLB{
    if (!_priceLB) {
        _priceLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 260, self.tableView.bottom + 20, 244, 18)];
        _priceLB.textColor = MainPureColor(85);
        _priceLB.font = FontSet(13);
        _priceLB.textAlignment = NSTextAlignmentRight;
    }
    return _priceLB;
}
- (UILabel *)paymentLB{
    if (!_paymentLB) {
        _paymentLB = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 260, self.priceLB.bottom + 11, 244, 18)];
        _paymentLB.textColor = RGB(255, 54, 43);
        _paymentLB.font = FontSet(13);
        _paymentLB.textAlignment = NSTextAlignmentRight;
    }
    return _paymentLB;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductOrderModel *model = self.dataSourceArr[indexPath.row];
    return model.refundbtn == YES ? 140:110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailGoodsTableViewCell"];
    if (!cell) {
        cell = [[DetailGoodsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetailGoodsTableViewCell"];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    
    return cell;
}





@end
